#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

pthread_mutex_t Mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  Cond=PTHREAD_COND_INITIALIZER;

int count=0;

void* routine(void* arg)
{
    pthread_detach(pthread_self());
    uint64_t number=(uint64_t)arg;
    while(1)
    {
        pthread_mutex_lock(&Mutex);
        pthread_cond_wait(&Cond,&Mutex);
        cout<<"pthread: "<<"thread-"<<number<<" , count: "<<count++<<endl;
        pthread_mutex_unlock(&Mutex);
    }
}



int main()
{
    for(uint64_t i=1;i<=4;i++)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,routine,(void*)i);
        sleep(1);
    }
    while(true)
    {
        sleep(1);
       // pthread_cond_signal(&Cond);
      //  cout<<"signal one thread..."<<endl;
        pthread_cond_broadcast(&Cond);
        cout<<"signall all thread..."<<endl;
    }
    return 0;
}


